package com.achun.getcouponleague.ui.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.achun.getcouponleague.R;
import com.achun.getcouponleague.presenter.impl.TiketPresenter;
import com.achun.getcouponleague.utils.Logutils;
import com.achun.getcouponleague.utils.PresenterManager;
import com.achun.getcouponleague.view.ITiketCallback;
import com.bumptech.glide.Glide;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class TiketActivity extends AppCompatActivity implements ITiketCallback {

    private TiketPresenter tiketPresenter;
    private Unbinder bind;
    @BindView(R.id.btn_back)
    public ImageView btn_back;
    @BindView(R.id.iv_cover_view)
    public ImageView iv_cover_view;
    @BindView(R.id.text_tiket_view)
    public TextView text_tiket_view;
    @BindView(R.id.take_tiket)
    public TextView take_tiket;
    private boolean hasTaobaoApp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tiket);
        bind = ButterKnife.bind(this);
        initView();
        initPresenter();
        initListener();
    }

    private void initListener() {
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        //点击领券监听事件
        take_tiket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //复制淘口令，有淘宝则跳转
                String ticketCode = text_tiket_view.getText().toString().trim();
                ClipboardManager clipboardManager = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
                //复制到剪贴板
                ClipData clipData = ClipData.newPlainText("taobao_ticket_code", ticketCode);
                clipboardManager.setPrimaryClip(clipData);
                //判断有无淘宝
                if (hasTaobaoApp){
                    //有则打开
                    Intent intent = new Intent();
                    ComponentName componentName = new ComponentName("com.taobao.taobao", "com.taobao.tao.welcome.Welcome");
                    intent.setComponent(componentName);
                    startActivity(intent);
                }else {
                    //无则提示复制成功
                    Toast.makeText(TiketActivity.this,"复制成功，打开淘宝领券吧",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        //判断是否有安装淘宝
        PackageManager pm = getPackageManager();
        try {
            PackageInfo packageInfo = pm.getPackageInfo("com.taobao.taobao", PackageManager.MATCH_UNINSTALLED_PACKAGES);
            hasTaobaoApp = packageInfo!=null;
        }catch (PackageManager.NameNotFoundException e){
            e.printStackTrace();
            hasTaobaoApp=false;
        }
        Logutils.e(this.getClass(),"有无淘宝呢--》"+hasTaobaoApp);
        take_tiket.setText(hasTaobaoApp?"打开淘宝领券":"复制淘宝口令");
//        ActivityRecord{d22350c u0 com.taobao.taobao/com.taobao.tao.welcome.Welcome t1593} time:117671798
    }

    private void initPresenter() {
         tiketPresenter = PresenterManager.getInstance().getTiketPresenter();
         tiketPresenter.registerCallback(this);
    }

    @Override
    public void onLoading() {

    }

    @Override
    public void onError() {

    }

    @Override
    public void onEmpty() {
        text_tiket_view.setText("来晚了，优惠券已被领完");
        take_tiket.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bind != null) {

            bind.unbind();
        }
        if (tiketPresenter!=null)
        tiketPresenter.unRegisterCallback(this);
    }

    @Override
    public void onShowTiket(String url, String tiketcode) {
        Logutils.e(this.getClass(),"ui加载数据"+url+"淘口令：--》"+tiketcode);

        Glide.with(this).load(url.startsWith("http")? url:"https:"+url).into(iv_cover_view);
        text_tiket_view.setText(tiketcode);
    }
}